<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册用户</title>
    <script src="/js/jquery/jquery-3.4.1.js"></script>
    <link rel="stylesheet" href="/css/common.css" type="text/css">
    <script src="/js/service/common.js"></script>
    <script>
        defaultBackPage();
        let oldPassword = "";
        let userId = "";
        const init = () => {
            $.ajax({
                url: "/user/getById/" + [[ ${res} ]],
                dataType: "json",
                type: "GET",
                success: function (json) {
                    if(json.code === 200){
                        let user = json.data;
                        let password = user.realPassword;
                        oldPassword = password;
                        userId = user.id;
                        $("#loginId").val(user.loginId);
                        $("#password").val(password);
                        $("#username").val(user.username);
                        let gender = user.gender;
                        if(gender === 1001){
                            $("#boy").attr("checked", true);
                        }else{
                            $("#girl").attr("checked", true);
                        }
                        $("#age").val(user.age)
                    }else{
                        tipsMessage(json);
                    }
                }
            });
        };
        window.onload = init;
        function save() {
            let newPassword = $("#password").val();
            let changePassword = false;
            if(newPassword !== oldPassword){
                if(confirm("检测到您已修改密码，保存信息后需要重新登陆，是否继续？")){
                    changePassword = true;
                }else{
                    return;
                }
            }
            $.ajax({
                url: "/user/saveDetail",
                data: JSON.stringify({
                    "id": userId,
                    "loginId": $("#loginId").val(),
                    "password": newPassword,
                    "username":$("#username").val(),
                    "gender":$("input[name=gender]:checked").val(),
                    "age":$("#age").val()
                }),
                contentType: 'application/json',
                type: "POST",
                dataType: "json",
                success: function (json) {
                    if(json.code === 200){
                        setUser();
                        if(changePassword){
                            window.location.href="/page/index";
                        }else{
                            window.location.href = window.location.protocol + '//' + window.location.host;
                        }
                    }else{
                        tipsMessage(json);
                    }
                },
                error: function () {
                    alert("提交失败");
                }
            })
        }
    </script>
</head>
<body>
    <object class="object-top" data="/page/common/top"></object>
    <div style="text-align: center; margin-top: 4%">
        <h1>用户信息详情</h1>
        <form id="form" style="margin-top: 4%">
            <label>账号</label>
            <label>
                <input name="loginId" type="text" id="loginId" readonly/>
            </label><br>
            <label>密码</label>
            <label>
                <input name="password" type="text" id="password"/>
            </label><br>
            <label>姓名</label>
            <label>
                <input name="username" type="text" id="username"/>
            </label><br>
            <label>性别</label>
            <label>
                <input name="gender" id="boy" type="radio" value="1001" />男
                <input name="gender" id="girl" type="radio" value="1002" />女
            </label><br>
            <label>年龄</label>
            <label>
                <input placeholder="请输入年龄" name="age" type="text" id="age" value="0"/>
            </label><br>
            <label><input type="button" class="btn" value="确定" onclick="save()"></label>
            <label><input type="button" class="btn" value="重置" onclick="init()"></label>
        </form>
    </div>
</body>
</html>